#include "inc/core/lib.h"
#include "math.h"
#include "string.h"
#include "stdio.h"

int token_file(char* short_file_name, char* name_pattern, char* delim, int** token_data, int token_data_size)
{
    int result = EXIT_FAILURE;

    char *name = strtok(short_file_name, delim);
    if (strcmp(name, name_pattern) == 0)
    {
        int i = 0;
        (*token_data) = (int*) malloc(token_data_size * sizeof (int));
        for (i = 0; i < token_data_size; i++)
        {
            char* digit = strtok(NULL, delim);
            (*token_data)[i] = atoi(digit);
            result = EXIT_SUCCESS;
        }
    }
    return result;
}

floatdig simple_integration(floatdig* x, floatdig*y, int num_of_points)
{
    int i = 0;
    floatdig result = 0;
    for (i = 1; i < num_of_points; i++)
    {
        result += 0.5 * (y[i - 1] + y[i])*(x[i - 1] - x[i]);
    }
    return result;
}

void lib_set_vector(vector3d* vec, floatdig val)
{
    if (vec != NULL)
    {
        int i = 0;
        for (i = 0; i < 3; i++)
        {
            vec->v[i] = val;
        }
    }
}

void lib_add_vector(vector3d* vec1, vector3d* vec2, vector3d* sum)
{
    int i = 0;
    for (i = 0; i < 3; i++)
    {
        sum->v[i] = vec1->v[i] + vec2->v[i];
    }
}

void lib_divide_vector(vector3d* in, floatdig div, vector3d* out)
{
    int i = 0;
    #ifdef VERBOSE
    if (div == 0)
    {
        printf("lib_divide_vector: division by zero!\n");
    }
    #endif
    for (i = 0; i < 3; i++)
    {
        out->v[i] = in->v[i]/div;
    }
}
